/*jslint vars:true plusplus:true devel:true*/

//=========================================================
// Interactive Digital Media Diploma
// CUFDIG404A_R1: Introduction to Scripting
// Assignment 3
// Author: Ben
// File: buggy.js
// Description: Buggy functionality
//=========================================================

// the player's buggy
function MoonBuggy(spriteSrc, jumpVelocity) {
    "use strict";
    var self = this;
        
    var horizontal_spd = 600;
    var left_boundary = 100;
    var right_boundary = 1000;
    var invincibletimer = 0;
    var flashtimer = 0;
    var startX = 100;
    var groundLevel = window.game.background.getGroundLevel();
    var bulletSprite = new Sprite("images/buggyBullet.png", function(sprite){
        sprite.offsetX = -32;
        sprite.offsetY = -32;
    }, 32, 32);
    var missilePool = new AmmoPool(3, BuggyMissile, new Sprite("images/Bullet 2.png"));
    var bulletPool = new AmmoPool(1, BuggyBullet, bulletSprite);
    var sprite = new Sprite(spriteSrc, function (sprite) {
        sprite.offsetX = -sprite.image.width / 2;
        sprite.offsetY = -sprite.image.height;
    });
    
    var entity = new Entity(sprite, startX, groundLevel);
    
    // the buggy has been hit
    this.onHit = function () {     
       
        if (invincibletimer <= 0){
        var audio = new Audio('audio/buggyexplode.ogg');
        audio.play();
    
        // on hit functionality here
        die(self); 
        }
         
    };

    
    var collider = new Collider(entity, 50, 0, 10, -30, self.onHit);
    var y_velocity = 0;
    var x_velocity = 0;
    var left = false;
    var right = false;
    var isdead = false;
    
    // make the buggy jump
    this.jump = function ()   {
    if(!isdead){
 
        
       
        // only jump on ground
        if (entity.y === groundLevel) {
            y_velocity = jumpVelocity;
            var audio = new Audio('audio/bov.ogg');
            audio.play();
        }
    }
    };
    
    this.setalive = function(enable){
        collider.isEnabled = enable;
        isdead = !enable;
        entity.isVisible = enable;
        entity.x = startX; 
        entity.y = groundLevel;
        invincibletimer = 5;
    }
    
    this.isAlive =function(){
        return !isdead;
    }
    
    this.getX = function(){
        return entity.x;
    }

    this.getY = function(){
        return entity.y;
    }

    // update the buggy every frame
    this.update = function () {
        
        
        
        
        // update ammo
        missilePool.update();
        bulletPool.update();
        if(!isdead){

            
            if (invincibletimer > 0){
                invincibletimer -= 1/window.game.frameRate;
                flashtimer -= 1/window.game.frameRate;
                
                if (flashtimer <= 0) {
                    entity.isVisible = !entity.isVisible;
                    flashtimer = 0.2;
                }
            }else
            {
                entity.isVisible = true;
            }

            
            x_velocity = 0;
            if (right)
            {
                x_velocity += horizontal_spd;
            }

            if (left)
            {
                x_velocity -= horizontal_spd;
            }


            // update velocity
            y_velocity += window.game.gravity / window.game.frameRate;

            // update y
            entity.y += y_velocity / window.game.frameRate;

            // update x
            entity.x += x_velocity / window.game.frameRate;



            // check for ground collision
            if (entity.y > groundLevel) {
                entity.y = groundLevel;
                y_velocity = 0;
            }

            // check for boundary hits
            if (entity.x < left_boundary)
                entity.x = left_boundary;

            if (entity.x > right_boundary)
                entity.x = right_boundary;
         }
     };
    
    // respond to key presses
    this.keyVal = function (keycode, value) {
        if (keycode === 32 && value) {
           if(!isdead){
               self.fire();
           } 
        }
        // respawn
            if (keycode === 13 && value) {
           if(isdead){
             alive(self);
           } 
        }
        
        // up arrow
        if (keycode === 38){
            self.jump();
        }
                
        // left arrow
        if (keycode === 37){
            left = value;
        }
        // right arrow
        if (keycode === 39){
            right = value;
        }
        
        
    };
    
    self.fire = function(){
        missilePool.fire(entity.x, entity.y - 40);
        bulletPool.fire(entity.x + 20, entity.y -20);
        var audio = new Audio('audio/buggygun.ogg');
        audio.play();
    };
    
    window.game.onClick = function(event){
        
         if(!isdead)
             self.fire();
    };
    
}

window.game.buggy = new MoonBuggy("images/buggy.png", -500);

// add the buggy as a subsystem
window.game.addSubsystem(window.game.buggy);



